package com.xxhui.recyclerptrdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.xxhui.recyclerptrdemo.Adapter.RecyclerViewAdapter;
import com.xxhui.recyclerptrdemo.Util.Recycler_ptr_util;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity_log";
    private RecyclerView recyclerView = null;
    private List<String> data;
    private RecyclerViewAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private PtrClassicFrameLayout mPtrFrame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        data = Recycler_ptr_util.getData();
        adapter = new RecyclerViewAdapter(data);
        recyclerView.setAdapter(adapter);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        mPtrFrame = (PtrClassicFrameLayout) findViewById(R.id.pull_to_refresh);
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new ImpPtrHandler());

        mPtrFrame.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPtrFrame.autoRefresh();
            }
        }, 100);
    }

    class ImpPtrHandler implements PtrHandler{

        @Override
        public void onRefreshBegin(PtrFrameLayout frame) {
            Recycler_ptr_util.updateData(data,adapter,mPtrFrame);
        }

        @Override
        public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
            //return true;
            return Recycler_ptr_util.checkRecyclerViewCanDoRefresh(recyclerView);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //add (int groupId, int itemId, int order, CharSequence title)
        menu.add(0,2,0, "GridLayout");
        menu.add(0,1,1,"LinearLayout");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                layoutManager = new LinearLayoutManager(this);
                recyclerView.setLayoutManager(layoutManager);
                break;
            case 2:
                layoutManager = new GridLayoutManager(MainActivity.this,3);
                recyclerView.setLayoutManager(layoutManager);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
